package com.ruike.alisurface.ui.setting;

import android.graphics.Color;
import android.os.Build;
import android.support.v4.widget.NestedScrollView;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.ruike.alisurface.Constant;
import com.ruike.alisurface.R;
import com.ruike.alisurface.Serials.MsgEventBus;
import com.ruike.alisurface.Serials.SerialPortInstructUtils;
import com.ruike.alisurface.Serials.Ttys3Utils;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.mqtt.MQTTService;
import com.ruike.alisurface.mqtt.ServiceUtils;
import com.ruike.alisurface.services.AppRunMonitoringService;
import com.ruike.alisurface.services.UvlampTimersService;
import com.ruike.alisurface.services.ZtlManager;
import com.ruike.alisurface.utils.DeviceUtils;
import com.ruike.alisurface.utils.ShareKey;
import com.ruike.alisurface.utils.TimeUtils;
import com.voodoo.lib_utils.L;
import com.voodoo.lib_utils.ShareUtils;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnLongClick;

/**
 * Author: voodoo
 * CreateDate: 2020/08/14 014 12:31 下午
 * Description: 其他设置
 */
public class OtherSettingActivity extends BaseAty implements L.OnLogCatListener {

    @BindView(R.id.otherSetting_lightOpenOrClone_swch)
    Switch lightOpenOrCloneSwch;
    @BindView(R.id.ohter_nestScrollview)
    NestedScrollView nestedScrollView;
    @BindView(R.id.tv_content)
    TextView tv_content;

    @BindView(R.id.other_signal_tv)
    TextView other_signal_tv;

    @BindView(R.id.other_Machine_info_tv)
    TextView mMachine_info;

    @BindView(R.id.otherSetting_zwlightOpenOrClone_swch)
    Switch mLamptOpenOrCloneSwch;
    @BindView(R.id.total_cycle_edt)
    EditText totalCycleEdt;
    @BindView(R.id.time_of_uv_lamp_edt)
    EditText timeOfUvLampEdt;
    @BindView(R.id.log_msg_ly)
    LinearLayout logMsgLy;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_setting_other;
    }

    @Override
    protected void initViews() {
        initTitleBar(false);
    }

    @Override
    protected void initData() {
        int totalcount = ShareUtils.getInstance().getInt(ShareKey.TOTAL_TIME, 3600) / 60;
        int countTime = ShareUtils.getInstance().getInt(ShareKey.INTERVAL_TIME, 120) / 60;
        totalCycleEdt.setText(totalcount + "");
        timeOfUvLampEdt.setText(countTime + "");
        int composed = ShareUtils.getInstance().getInt(ShareKey.BOARD_NUM, 1);

        Constant.showShop = true;
        lightOpenOrCloneSwch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //                                         电路板地址， 开关模式， 通道0， 开灯， 开关模式下无所谓的参数， 同前一个
                    Ttys3Utils.getInstance().LEDSetting(10, "0", "0", "1", 70, 15);
                } else {
                    Ttys3Utils.getInstance().LEDSetting(10, "0", "0", "0", 70, 15);
                }
            }
        });
        mMachine_info.setText(String.format("  机   器SN :  %s    \n  手机卡号 :  %s    \n  固件信息 :  %s",
                DeviceUtils.getDeviceSn(), DeviceUtils.getSimSerialNumber(), Build.FINGERPRINT));
        boolean isuvLampService = ShareUtils.getInstance().getBoolean("isuvLampService", false);
        L.setOnLogCatListener(this);
        mLamptOpenOrCloneSwch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                L.i("==紫外灯 开关启动按钮   =isChecked=" + isChecked);
                ShareUtils.getInstance().putBoolean("isuvLampService", isChecked);
                Constant.showShop = false;
                if (isChecked) {
                    ServiceUtils.startService(OtherSettingActivity.this, UvlampTimersService.class);
                } else {
                    ServiceUtils.stopService(OtherSettingActivity.this, UvlampTimersService.class);
                }

                int IO2 = ZtlManager.getInstance().smdtReadExtrnalGpioValue(2);
                L.i("==人体感应状态-  IO2-==", IO2);
            }
        });
        mLamptOpenOrCloneSwch.setChecked(isuvLampService);
    }

    StringBuffer sbf = new StringBuffer();

    @Override
    public void doEventBusResult(MsgEventBus event) {
        if (event.getType().equals("温控返回")) {
            sendDataSbf(event.getMessage());
            UnpdataTextV();
            return;
        }
        if ("网络信号强度".equals(event.getType())) {
            other_signal_tv.setText("网络信号强度：" + event.getMessage());
            return;
        }
    }

    @OnClick({R.id.otherSetting_cool_btn, R.id.otherSetting_hot_btn, R.id.otherSetting_close_btn, R.id.otherSetting_goback_imgv,
            R.id.other_stopmqtt_btn, R.id.other_stopapp_btn,
    })
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.otherSetting_cool_btn:
                showLongTip("点击了制冷", 6000);
                sendDataSbf(SerialPortInstructUtils.temperatureControl(0, 1));
                Ttys3Utils.getInstance().SendPort(SerialPortInstructUtils.temperatureControl(0, 1));
                UnpdataTextV();
                break;
            case R.id.otherSetting_hot_btn:
                showLongTip("点击了制热", 2000);
                sendDataSbf(SerialPortInstructUtils.temperatureControl(0, 40));

                Ttys3Utils.getInstance().SendPort(SerialPortInstructUtils.temperatureControl(1, 40));
                UnpdataTextV();
                break;
            case R.id.otherSetting_close_btn:
                showLongTip("点击了停止", 2000);
                sendDataSbf(SerialPortInstructUtils.temperatureControl(3));
                Ttys3Utils.getInstance().SendPort(SerialPortInstructUtils.temperatureControl(3));
                UnpdataTextV();
                break;
            case R.id.otherSetting_goback_imgv:
                finish();
                break;

            case R.id.other_stopmqtt_btn:

                ServiceUtils.stopService(this, MQTTService.class);

                break;
            case R.id.other_stopapp_btn:

                ServiceUtils.stopService(this, AppRunMonitoringService.class);

                break;

            default:
                break;
        }
    }


    @OnLongClick(R.id.tv_content)
    public boolean onLongClicks(View view) {
        switch (view.getId()) {
            case R.id.tv_content:
                sbf.delete(0, sbf.length());
                UnpdataTextV();
                break;
            default:
                break;
        }
        return false;

    }

    public void UnpdataTextV() {
        tv_content.setText(sbf.toString());
        nestedScrollView.fullScroll(NestedScrollView.FOCUS_DOWN);
    }

    public void sendDataSbf(String str) {
        sbf.append(TimeUtils.millis2String(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss:SSS")).append(":   ").append(str).append("\n");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Constant.showShop = false;
    }

    @Override
    public void onShowLog(String logMsg) {
        if (logMsg.contains("紫外灯=")) {
            int count = logMsgLy.getChildCount();
            if (count > 500) {
                logMsgLy.removeViews(0, 400);
            }
            TextView textView = new TextView(this);
            textView.setTextColor(Color.parseColor("#1139BE"));
            textView.setTextSize(20);
            textView.setText(logMsg);
            logMsgLy.addView(textView);
            nestedScrollView.fullScroll(NestedScrollView.FOCUS_DOWN);
        }
    }
}
